package com.zzptc.fxh.myapplication;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import org.litepal.LitePal;

import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private final List<RecipeResult> rvData;
    private final Context mContext;

    public MyAdapter(Context context){
        rvData= LitePal.findAll(RecipeResult.class);
        mContext=context;
    }
    @NonNull
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(mContext).inflate(R.layout.rv_item,parent,false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
        RecipeResult itemData=rvData.get(position);
        Glide.with(mContext)
                .load(itemData.getCover())
                .into(holder.ivCover);
        holder.tvTitle.setText(itemData.getTitle());
    }

    @Override
    public int getItemCount() {
        return rvData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView ivCover;
        private final TextView tvTitle;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ivCover=itemView.findViewById(R.id.iv_cover);
            tvTitle=itemView.findViewById(R.id.tv_title);
        }
    }
}
